import request from "../utils/request";
import { useUserStore } from '../stores/user'
import type { UserData } from "../types/user";
import type { BaseResponse } from '../types/api-response'


// 登录接口（data: string）
export const login = (username: string, password: string) => {
  const params = new URLSearchParams();
  params.append('username', username);
  params.append('password', password);

  return request<BaseResponse<string>>({ // 明确指定data类型为string
    url: "/user/login",
    method: "post",
    data: params,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
  });
};

// 手机号登录接口（data: string）
export const PhoneLogin = (phone: string, code: string) => {
  const params = new URLSearchParams();
  params.append('phone', phone);
  params.append('code', code);

  return request<BaseResponse<string>>({
    url: "/user/loginByPhone",
    method: "post",
    data: params,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
  });
};

// 获取验证码接口（data: string）
export const requestVerificationCode = (phone: string) => {
  const params = new URLSearchParams();
  params.append('phone', phone);
  return request<BaseResponse<string>>({
    url: "/user/code",
    method: "post",
    data: params,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
  });
};

// 注册接口（data: string）
export const register = (username: string, password: string) => {
  const params = new URLSearchParams();
  params.append('username', username);
  params.append('password', password);

  return request<BaseResponse<string>>({
    url: "/user/register",
    method: "post",
    data: params,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
  });
};

// 获取用户信息接口（data: UserData）
export const getUserInfo = (id: string) => {
  const userStore = useUserStore()
  return request<BaseResponse<UserData>>({
    url: "/user/info",
    method: "get",
    params: { id },
    headers: { 'token': userStore.token }
  });
};

